package com.yin.jia.liang.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yin.jia.liang.common.Config;
import com.yin.jia.liang.dao.TeachMapper;
import com.yin.jia.liang.entity.Teach;
import com.yin.jia.liang.entity.TeachExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class TeachService {

    @Autowired
    TeachMapper teachMapper;

    public Teach findById(Integer id) {
        return teachMapper.selectByPrimaryKey(id);
    }

    public List<Teach> findByUserId(int userId) {
        TeachExample ue = new TeachExample();
        TeachExample.Criteria criteria = ue.createCriteria();
        criteria.andUserIdEqualTo(userId);
        ue.setOrderByClause("id desc");
        List<Teach> g = teachMapper.selectByExample(ue);

        return g;
    }

    public PageInfo<Teach> pageFindByExample(int currentPage, Teach teach) {

        TeachExample ue = new TeachExample();
        TeachExample.Criteria criteria = ue.createCriteria();


        ue.setOrderByClause("id desc");

        PageHelper.startPage(currentPage, Config.PAGE_SIZE);
        List<Teach> g = teachMapper.selectByExample(ue);
        PageInfo<Teach> pageInfo = new PageInfo<>(g);

        return pageInfo;
    }

    public Integer save(Teach teach) {
        int i = teachMapper.insert(teach);
        return i;
    }

    public int update(Teach teach) {
        int i = teachMapper.updateByPrimaryKeySelective(teach);
        return i;
    }

    public Integer delete(Integer id) {
        return teachMapper.deleteByPrimaryKey(id);
    }
}
